package com.koron.order.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.BaseQueryBean;
import com.koron.common.core.util.CodeTools;
import com.koron.order.change.dispatch.mapper.ChangeOrderDispatchTeamMapper;
import com.koron.order.change.util.ThreadLocalOrderChange;
import com.koron.order.common.bean.base.OrderType;
import com.koron.order.common.bean.po.OrderDispatchTeam;
import com.koron.order.common.mapper.OrderDispatchTeamMapper;
import com.koron.order.common.service.OrderDispatchTeamService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (OrderDispatchTeam)表服务实现类
 *
 * @author makejava
 * @since 2021-09-03 15:59:47
 */
@Service("orderDispatchTeamService")
public class OrderDispatchTeamServiceImpl implements OrderDispatchTeamService {

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, OrderDispatchTeam orderDispatchTeam) {
        if (StringUtils.isEmpty(orderDispatchTeam.getId())) {
            orderDispatchTeam.setId(CodeTools.getCode32());
        }
        return getOrderDispatchTeamMapper(factory).insert(orderDispatchTeam);
    }

    @Override
    public int insertBatch(SessionFactory factory, List<OrderDispatchTeam> orderDispatchTeams) {
        if (CollectionUtils.isEmpty(orderDispatchTeams)) {
            return 0;
        }
        OrderDispatchTeamMapper mapper = getOrderDispatchTeamMapper(factory);
        mapper.deleteByDispatchId(orderDispatchTeams.get(0).getDispatchId());
        orderDispatchTeams.forEach(p -> {
            if (StringUtils.isEmpty(p.getId())) {
                p.setId(CodeTools.getCode32());
            }
        });
        return mapper.insertBatch(orderDispatchTeams);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, OrderDispatchTeam orderDispatchTeam) {
        return getOrderDispatchTeamMapper(factory).update(orderDispatchTeam);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {
        return getOrderDispatchTeamMapper(factory).deleteById(id);
    }

    @Override
    @TaskAnnotation("deleteByOrderId")
    public int deleteByOrderId(SessionFactory factory, String orderId) {
        return getOrderDispatchTeamMapper(factory).deleteByOrderId(orderId);
    }

    @Override
    @TaskAnnotation("queryById")
    public OrderDispatchTeam queryById(SessionFactory factory, String id) {
        return getOrderDispatchTeamMapper(factory).queryById(id);
    }

    @Override
    public List<OrderDispatchTeam> queryByDispatchId(SessionFactory factory, String dispatchId) {
        return getOrderDispatchTeamMapper(factory).queryByDispatchId(dispatchId);
    }

    @Override
    @TaskAnnotation("queryList")
    public PageInfo queryList(SessionFactory factory, OrderDispatchTeam orderDispatchTeam, BaseQueryBean pageBean) {
        PageHelper.startPage(pageBean.getPage(), pageBean.getPageSize());
        return new PageInfo(getOrderDispatchTeamMapper(factory).queryList(orderDispatchTeam));
    }

    OrderDispatchTeamMapper getOrderDispatchTeamMapper(SessionFactory factory) {

        if (OrderType.CHANGE.equals(ThreadLocalOrderChange.get())) {
            return factory.getMapper(ChangeOrderDispatchTeamMapper.class);
        }

        return factory.getMapper(OrderDispatchTeamMapper.class);
    }

}